package com.yinshan.voiceplay;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.SoundPool;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;

import com.yinshan.voiceplay.utils.MoneyUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author limh
 * @function
 * @date 2020/7/17 14:37
 */
@RequiresApi(api = Build.VERSION_CODES.N)
public class VoiceSpeaker {
    private final static String TAG = "PlayUtils";

    private static VoiceSpeaker sInstance;
    private ExecutorService service;
    private AudioAttributes audioAttributes;
    private Context context;
    private List<Integer> voices = new ArrayList<>();

    private VoiceSpeaker(Context context) {
        this.context = context;
        service = Executors.newCachedThreadPool();
        audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_MEDIA)
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .build();
    }


    public static synchronized VoiceSpeaker getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new VoiceSpeaker(context);
        }
        return sInstance;
    }


    public VoiceSpeaker addVoice(String voiceString) {
        if (null == voices) {
            voices = new ArrayList<>();
        }
        voices.clear();
        //‘收款成功’语音索引
        voices.add(R.raw.tts_success);
        String money = MoneyUtils.getMoney(voiceString);
        if (TextUtils.isEmpty(money)) {
            return this;
        }
        //金额转换为语音索引
        if (money.contains(".")) {
            String[] tmp = money.split("\\.");
            voices.addAll(MoneyUtils.readIntPart(tmp[0]));
            if (!MoneyUtils.readDecimalPart(tmp[1]).isEmpty()) {
                voices.add(R.raw.tts_dot);
                voices.addAll(MoneyUtils.readDecimalPart(tmp[1]));
            }
        } else {
            voices.addAll(MoneyUtils.readIntPart(money));
        }
        //‘元’语音索引
        voices.add(R.raw.tts_yuan);
        return this;
    }

    public void speak() {
        if (service != null) {
            service.execute(new Runnable() {
                @Override
                public void run() {
                    start();
                }
            });
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void start() {
        if (voices.isEmpty()) {
            return;
        }
        final List<Integer> soundIds = new ArrayList<>();
        SoundPool soundPool = new SoundPool.Builder()
                .setAudioAttributes(audioAttributes)
                .setMaxStreams(1)
                .build();
        for (Integer item : voices) {
            int soundId = soundPool.load(context, item, 1);
            soundIds.add(soundId);
        }
        soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(final SoundPool soundPool, int sampleId, int status) {
                for (int i = 0; i < soundIds.size(); i++) {
                    soundPool.play(soundIds.get(i), 6, 6, 1, 0, 1.0f);
                    int delpy = 250;
                    if (i == 0 || i == soundIds.size() - 1) {
                        delpy = 1000;
                    }
                    try {
                        Thread.sleep(delpy);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                soundPool.release();
            }
        });

    }
}
